import {GeneratedType, Registry} from "@cosmjs/proto-signing";

require("dotenv").config();

const { IndexedTx,SigningStargateClient, StargateClient } = require("@cosmjs/stargate")
const { MsgReceiveMessage } = require("../generated/circle/cctp/v1/tx")

const config = require('../config');

const rpc = config.noble.url;


const {     eth_caller_wallet,
    fn_noble_caller,
    fn_noble_receipter} = require('../wallet');

export const cctpTypes: ReadonlyArray<[string, GeneratedType]> = [
    ["/circle.cctp.v1.MsgReceiveMessage", MsgReceiveMessage],
];

function createDefaultRegistry(): Registry {
    return new Registry(cctpTypes)
}


const mint = async () =>{
    let caller_wallet = await fn_noble_caller();
    const [caller] = await caller_wallet.getAccounts();
    console.log('caller.address: ', caller.address);

    const client_caller = await SigningStargateClient.connectWithSigner(rpc, caller_wallet, {registry: createDefaultRegistry()});

    let attestation = "d18c8ffc467c6620723149e5d9f3cde9810bc9a8fc4bb515b2deefa5fa70d5db5e1809c06492eaa90efbc5ed381d8eaffe78ba486672a31065f16bff9aa193f81b02220e21d71dd4680b8b2daeb184fab1ec7d9e216af5ea889306357a37cb6ba71c622660d790d50160d360355ef1fbcde8fdb651bd7697bfdc72b512befebe441c";
    let message = "000000000000000000000004000000000003ae05000000000000000000000000d0c3da58f55358142b8d3e06c1c30c5c6114efe800000000000000000000000057d4eaf1091577a6b7d121202afbd2808134f117000000000000000000000000e97e4b1538f1401f1772779227edfa02e466e37f0000000000000000000000000000000007865c6e87b9f70255377e024ace6630c1eaa37f0000000000000000000000007a876149cf0fbba5abc4741084495b95c43e8c1f00000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000ef48c3ab7f184e469504bdbd9c07883ab4427c66";

    const attest_buffer = Buffer.from(attestation, "hex")
    const message_buffer = Buffer.from(message, "hex")
    const msg = {
        typeUrl:"/circle.cctp.v1.MsgReceiveMessage",
        value:{
            from:caller.address,
            message:message_buffer,
            attestation:attest_buffer,
        }
    }
    const fee = {
        amount: [
            {
                denom: "uusdc",
                amount: "200000",
            },
        ],
        gas: "200000",
    };
    const memo = "haolf";
    const result = await client_caller.signAndBroadcast(
        caller.address,
        [msg],
        fee,
        memo
    );
    console.log("result: ", result)
}
mint();

/**
 * 4FC2B12B04AD3C48AB3FCB6A7DE22725BACECF100F5058C8BE6514F4B247241C
 *
 */
